package com.woniu.controller;

import com.woniu.entity.User;
import com.woniu.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
//@CrossOrigin
public class UserController {
    @Resource
    private UserService userService;
    private User use;

    @RequestMapping("/findAll")
    public List<User> findAll(){
        return userService.findAll();
    }
    @RequestMapping("/add")
    //404找不到资源 400参数错误 405请求凡是不正确 500代码错误
    public Integer addUser(User user){
        return userService.add(user);
    }
    @RequestMapping("/update")
    public Integer update(User user){
        return userService.update(user);
    }
    @RequestMapping("/delete")
    public Integer delete(Integer id){
        return userService.delete(id);
    }
    @RequestMapping("/findById")
    public User findById(Integer id){
        return userService.findById(id);
    }
    @RequestMapping("/findByCondition")
    public List<User> findByCondition(User user){
       return userService.findByCondition(user);
    }
    @RequestMapping(value = "/login",method = RequestMethod.POST )
//    @RequestBody 表示接受json格式的请求参数
    public Map<String,Object> login(@RequestBody User user) throws Exception {
        User loginUser = userService.findByName(user.getName());
        HashMap<String, Object> map = new HashMap<>();
        if (loginUser==null) {
            throw  new Exception("用户不存在");
        } else if (!loginUser.getPassword().equals(user.getPassword())) {
            throw new Exception("密码不正确");
        }else {
            map.put("code",200);
            map.put("msg","登录成功");
            map.put("data",loginUser);
        }
        return map;
    }

}
